/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Ventas;

import Atxy2k.CustomTextField.RestrictedTextField;
import javax.swing.JDialog;
import javax.swing.table.AbstractTableModel;
import java.util.Date;
import java.util.ArrayList;
import Controlador.Service.Ventas.GestionVentas;
import Controlador.Bean.Ventas.PedidoBusqueda;
import javax.swing.table.TableRowSorter;


/**
 *
 * @author Luis Enrique
 */
public class PedidoBuscarPanel extends javax.swing.JPanel {

    PedidoTableModel myTableModel;
    private JDialog parent;
    private FacturacionRegistrarPanel refPanel;
    Date fecha = new Date ();
    PedidoBusqueda pedidoBusqueda = new PedidoBusqueda(null,0,null,0,"Registrado",null,0,null,null,null,null,null,null,null,null,null);
    ArrayList<PedidoBusqueda> pedidosConsulta = GestionVentas.getInstance().queryPedidos(pedidoBusqueda);
    
    public void setParent(JDialog parent){
        this.parent = parent;
    }
    
    public void setRefPanel(FacturacionRegistrarPanel panel){
        this.refPanel = panel;
    }
        
    class PedidoTableModel extends AbstractTableModel{
        String [] titles = {"Número de Pedido", "Número de Cotización","Razón Social/Nombre","Monto Total",
                            "Estado"};

        @Override
        public int getRowCount() {
            return pedidosConsulta.size();
        }

        @Override
        public int getColumnCount() {
            return titles.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            PedidoBusqueda pb = pedidosConsulta.get(rowIndex);
            switch(columnIndex){
                case 0: res = pb.getNumeroPed()+""; break;
                case 1: res = pb.getNumeroCot()+"";break;
                case 2: res = (pb.getRazonSocial()!=null)?pb.getRazonSocial():(pb.getApePaterno()+" "+pb.getApeMaterno()+", "+pb.getNombres()); break;
                case 3: res = pb.getTotal()+""; break;
                case 4: res = pb.getEstado();break;
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return titles[col];
        }
    }
    
    public PedidoBuscarPanel() {
        initComponents();
        
        RestrictedTextField restricted = new RestrictedTextField(txtNombre);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtNumDoc);
        restricted.setLimit(11);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtNumPedido);
        restricted.setLimit(6);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtNumCotizacion);
        restricted.setLimit(6);
        restricted.setOnlyNums(true);
        
        myTableModel = new PedidoTableModel();
        tbPedidos.setModel(myTableModel);
        TableRowSorter<PedidoTableModel> sorter = new TableRowSorter<PedidoTableModel> ((PedidoTableModel) tbPedidos.getModel());
        tbPedidos.setRowSorter(sorter);
    }

    public void refreshItemsTable(){
        PedidoBusqueda pedidoBusqueda2 = new PedidoBusqueda(null,0,null,0,"Registrado",null,0,null,null,null,null,null,null,null,null,null);
        this.pedidosConsulta = GestionVentas.getInstance().queryPedidos(pedidoBusqueda2);
        myTableModel.fireTableChanged(null);        
    }
    
    public void refreshItemsTable2(ArrayList<PedidoBusqueda> pedidosBusq){
        this.pedidosConsulta = new ArrayList(pedidosBusq);
        myTableModel.fireTableChanged(null);        
    }
    
    public void limpiarCriterios(){
        txtNumPedido.setText("");
        txtNombre.setText("");
        txtNumDoc.setText("");
        txtNumCotizacion.setText("");
        dpFecha.setDate(null);
    }
                    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbPedidos = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNumDoc = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtNumPedido = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        dpFecha = new org.jdesktop.swingx.JXDatePicker();
        jLabel5 = new javax.swing.JLabel();
        txtNumCotizacion = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(784, 549));
        setMinimumSize(new java.awt.Dimension(1, 1));
        setPreferredSize(new java.awt.Dimension(784, 549));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Cotizaciones"));
        jPanel1.setToolTipText("");
        jPanel1.setMaximumSize(new java.awt.Dimension(764, 305));
        jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanel1.setPreferredSize(new java.awt.Dimension(764, 305));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tbPedidos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbPedidos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPedidosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbPedidos);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Información del Pedido"));

        jLabel1.setText("Razón Social/Nombre");

        jLabel2.setText("Número de Documento");

        jLabel10.setText("Número de Pedido");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtNumPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Información de la Cotización"));

        jLabel3.setText("Fecha");

        jLabel5.setText("Número de la Cotización");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtNumCotizacion)
                    .addComponent(dpFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumCotizacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(btnBuscar)
                                .addGap(18, 18, 18)
                                .addComponent(btnLimpiar))))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnLimpiar)
                            .addComponent(btnBuscar))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(133, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
            long numeroPed = (txtNumPedido.getText().equals(""))?0:Long.parseLong(txtNumPedido.getText());
            String razonSocial = (txtNombre.getText().equals(""))?null:"%"+txtNombre.getText()+"%";
            String numeroDoc = (txtNumDoc.getText().equals(""))?null:txtNumDoc.getText();
            long numeroCot = (txtNumCotizacion.getText().equals(""))?0:Long.parseLong(txtNumCotizacion.getText());
            Date fechaC = (dpFecha.getDate()==null)?null:dpFecha.getDate();
            
            PedidoBusqueda pedidoBusq = new PedidoBusqueda(null,numeroPed,fechaC,0,"Registrado",null,numeroCot,null,null,null,razonSocial,razonSocial,razonSocial,razonSocial,null,numeroDoc);
            ArrayList<PedidoBusqueda> cotizacionesBusq = Controlador.Service.Ventas.GestionVentas.getInstance().queryPedidos(pedidoBusq);
            refreshItemsTable2(cotizacionesBusq);
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiarCriterios();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void tbPedidosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPedidosMouseClicked
        if(evt.getClickCount()==2){
            int rowSel = tbPedidos.getSelectedRow();
            refPanel.pedido = pedidosConsulta.get(rowSel);     
            refPanel.llenaPedido();
            refPanel.habilita();
            parent.dispose();           
        }
    }//GEN-LAST:event_tbPedidosMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnLimpiar;
    private org.jdesktop.swingx.JXDatePicker dpFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbPedidos;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumCotizacion;
    private javax.swing.JTextField txtNumDoc;
    private javax.swing.JTextField txtNumPedido;
    // End of variables declaration//GEN-END:variables
}
